package registnet.base.source;

import grammar.PrerequisiteCruncher;

import java.util.List;
import java.util.ListIterator;

import registnet.base.CourseWork;
import registnet.base.Curriculum;
import registnet.base.DefaultPlanOfCourseWork;
import registnet.base.IPlanOfCourseWork;
import registnet.base.Prerequisite;
import registnet.dao.Course;
import registnet.dao.CoursePeer;
import registnet.dao.PlanOfCourseWork;
import registnet.dao.PlanOfCourseWorkPeer;
import registnet.dao.Student;
import registnet.dao.StudentPeer;

/**
 * Defines a class for creating a <code>PlanOfCourseWork</code> from the data from a specific source.
 */
public class PlanOfCourseWorkSource implements IPlanOfCourseWorkSource {

	/**
	 * The plan of coursework.
	 */
	private DefaultPlanOfCourseWork planOfCourseWork;
	
	/**
	 * The student number of the student associated with the plan of coursework.
	 */
	private String studentNumber;
	
	/**
	 * Creates a new plan of coursework for a student with the given student number.
	 * @param studentNumber
	 */
	public PlanOfCourseWorkSource(String studentNumber){
		this.studentNumber = studentNumber;
		load();
	}
	
	@Override
	public IPlanOfCourseWork getPlanOfCourseWork() {
		return planOfCourseWork;
	}

	@Override
	public IPlanOfCourseWork load() {
		List<PlanOfCourseWork> daoPlanOfCourseWorks;
		
		if (planOfCourseWork != null) return planOfCourseWork;
		else planOfCourseWork = new DefaultPlanOfCourseWork();
		
		try{
			//studentNumber
			Student daoStudent = new StudentPeer().getStudent(studentNumber);
			planOfCourseWork.setStudentNumber(studentNumber);
			
			//curriculum
			CurriculumSource curriculumSource = new CurriculumSource(daoStudent.getCurriculum().getCurriculumCode());
			Curriculum curriculum = (Curriculum)curriculumSource.getCurriculum();
			planOfCourseWork.setCurriculum(curriculum);
			
			//courses
			daoPlanOfCourseWorks = new PlanOfCourseWorkPeer().getPlanOfCourseWorkByStudent(daoStudent.getStudentId());
			for(int i=0; i<daoPlanOfCourseWorks.size(); i++){
				CourseWork course = new CourseWork();
				PlanOfCourseWork daoPlanOfCourseWork = daoPlanOfCourseWorks.get(i);
				
				//courseCode
				course.setCourseCode(daoPlanOfCourseWork.getCourse().getCourseCode());
				
				//courseName
				course.setCourseName(daoPlanOfCourseWork.getCourse().getCourseName());
				
				//prerequisites
				Course daoCourse = new CoursePeer().getCourse(daoPlanOfCourseWork.getCourse().getCourseCode());
				List<String> prerequisiteList = PrerequisiteCruncher.getPrerequisites(daoCourse.getPrerequisite());
				ListIterator<String> temp = prerequisiteList.listIterator();
				while(temp.hasNext()){
					Prerequisite prerequisite = new Prerequisite(temp.next());
					course.addPrerequisite(prerequisite);
				}
				
				//unit
				course.setUnit(daoPlanOfCourseWork.getCourse().getUnits());
				
				//semester
				course.setSemester(daoPlanOfCourseWork.getSemester());
				
				//year
				course.setYear(daoPlanOfCourseWork.getYear());
				
				//type
				course.setType(daoPlanOfCourseWork.getType());

				planOfCourseWork.addCourse(course);
			}
			
		}catch(Exception e){
			System.err.println("****[error] Student("+studentNumber+") plan of coursework loading failed. "+e.getMessage());
		}
		
		return planOfCourseWork;
	}

}
